package com.wrxdz.start.surfaceviewt;

import android.app.Application;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class ApplicationUtil extends Application {
        public static String ADDRESS = "192.168.0.127";
//    public static String ADDRESS = "192.168.43.240";
    public static final int PORT = 30000;
    private Socket socket = null;
    private boolean connect = false;
    private boolean canPlay = false;

    public void init() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //与服务器建立连接
                try {
                    socket = new Socket();
                    socket.connect(new InetSocketAddress(ADDRESS, PORT), 8000);
                    connect = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public Socket getSocket() {
        return socket;
    }

    public boolean isConnect() {
        return connect;
    }

    public void setCanPlay(Boolean canPlay) {
        this.canPlay = canPlay;
    }

    public boolean isCanPlay() {
        return canPlay;
    }

    public void closeSocket() {
        if (socket.isConnected()) {
            try {
                socket.close();
                socket = null;
                connect = false;
                canPlay = false;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
